<?php
namespace Home\Controller;
use Think\Controller;

/**
 * 商家登陆获取appid
 */
class ChannelIdController extends Controller{
	/**
	 * 商家登陆 username,password,channelId, landstate
	 */

		public function login()
		{
			$uname=I('request.username','','strip_tags');
			$pass=I('request.password','','strip_tags');
			$landstate=I('request.landstate','','strip_tags');///登陆状态
			//$channelId=I('request.channelId','','strip_tags');
			$channelId=!empty($_GET['channelId'])?$_GET['channelId']:'';
	        $txt = '  '.date('Y-m-d H:i:s', time()).' uname='.$uname.'pass='.$pass.'landstate='.gettype($landstate).$landstate.'channelId='.gettype($channelId).$channelId.'|';
            file_put_contents("./Public/newfile.text",$txt, FILE_APPEND);
			  if(empty($landstate)||empty($channelId) || $channelId=='null' ||$channelId=='(null)')
			  {
				$re['status']=3;
				$re['msg']="参数有误";
				echo json_encode($re,JSON_UNESCAPED_UNICODE);
				return '';

			 }
			
		 	$shopid=M('shop_user')->where("username='%s' and password='%s'",$uname,$pass)->getField("shopid");
			if(empty($shopid))
			{
				$re['status']=0;
				$re['msg']="用户名密码有误";
				echo json_encode($re,JSON_UNESCAPED_UNICODE);
				return '';
			}

			if($landstate=='ios')
			{
				$ichannelId=M('shop_user')->where("shopid='%s'",$shopid)->getField("ichannelId");
					if(empty($ichannelId))
					{
						$map['ichannelId']=I('request.channelId','','strip_tags');
						$map['landstate']='ios';
						$re=M('shop_user')->where("shopid='%d'",$shopid)->save($map);
						
						$result['status']=1;
						$result['msg']='合法用户';
						$result['shopid']=$shopid;

						echo json_encode($result,JSON_UNESCAPED_UNICODE);
						return '';

					}

					if($ichannelId!=$channelId)
					 {
							$result['status']=2;
							$result['msg']='已经有ios设备登陆';
							echo json_encode($result,JSON_UNESCAPED_UNICODE);
							return '';

					}

					if($ichannelId==$channelId)
					{
						$map['ichannelId']=I('request.channelId','','strip_tags');
						$map['landstate']='ios';
						$re=M('shop_user')->where("shopid='%d'",$shopid)->save($map);
						$result['status']=1;
						$result['msg']='合法用户';
						$result['shopid']=$shopid;

						echo json_encode($result,JSON_UNESCAPED_UNICODE);
						return '';
					}
			}

					
						 
					
			if($landstate=='android')
			{
			
				$achannelId=M('shop_user')->where("shopid='%s'",$shopid)->getField("achannelId");
				if(empty($achannelId))
				{
					$data['achannelId']=I('request.channelId','','strip_tags');
					$data['landstate']='android';
					$re=M('shop_user')->where("shopid='%d'",$shopid)->save($data);
					
					$result['status']=1;
					$result['msg']='合法用户';
					$result['shopid']=$shopid;
					echo json_encode($result,JSON_UNESCAPED_UNICODE);
					return '';

				}

				if($achannelId!=$channelId)
				{
							$result['status']=2;
							$result['msg']='已经有android设备登陆';
							echo json_encode($result,JSON_UNESCAPED_UNICODE);

				}
				if($achannelId ==$channelId)
				{
					$data['achannelId']=I('request.channelId','','strip_tags');
					$data['landstate']='android';
					$re=M('shop_user')->where("shopid='%d'",$shopid)->save($data);

					$result['status']=1;
					$result['msg']='合法用户';
					$result['shopid']=$shopid;
					echo json_encode($result,JSON_UNESCAPED_UNICODE);
					return '';
				}

					
			}

		}
    /**
     * 退出登录
     */
    public function logout(){
   	$shopid=I('request.shopid','','strip_tags');
   	$landstate=I('request.landstate','','strip_tags');///登陆状态
   	$txt = '  '.date('Y-m-d H:i:s', time()).'landstate='.gettype($landstate).$landstate.'|'.'shopid='.$shopid;
             file_put_contents("./Public/logout.text",$txt, FILE_APPEND);


   	 if(empty($shopid)||empty($landstate) || $landstate=='null' ||$landstate=='(null)' ||$shopid=='null' ||$shopid=='(null)')
			  {
				$re['status']=3;
				$re['msg']="参数有误";
				echo json_encode($re,JSON_UNESCAPED_UNICODE);
				return '';

			 }

   	

   	///$map['landstate']=2;
   	if($landstate=='ios'){
   		$map['landstate']='';
   	     $map['ichannelId']='';
   		$re=M('shop_user')->where("shopid='%d'",$shopid)->save($map);
        
   			if($re===false){
   		$result['status']=0;
   		$result['msg']='操作失败';
   		 echo json_encode($result,JSON_UNESCAPED_UNICODE);
   	}else{
   		$result['status']=1;
   		$result['msg']='退出登录成功';
   		 echo json_encode($result,JSON_UNESCAPED_UNICODE);

   	}


   		
   	}
   	if($landstate=='android'){
   		$data['landstate']='';
   	     $data['achannelId']='';
   		$res=M('shop_user')->where("shopid='%d'",$shopid)->save($data);
   			if($res===false){
   		$result['status']=0;
   		$result['msg']='操作失败';
   		 echo json_encode($result,JSON_UNESCAPED_UNICODE);
   	}else{
   		$result['status']=1;
   		$result['msg']='退出登录成功';
   		 echo json_encode($result,JSON_UNESCAPED_UNICODE);

   	}


   	}
   	
   

   }

  /**
   * 推送  itemid deviceid
   */
	  public function push(){
		$android=new \Home\Controller\BaiduPushController();
		$ios=new \Home\Controller\BaiduPushForIosController();
	  	$itemid=I('get.itemid','','strip_tags');
	  	$where['devicecode']=I('get.deviceid','','strip_tags');////机器设备号
       
        if(empty($itemid)||empty($where)){
            $re['status']=3;
                $re['msg']="参数有误";
                echo json_encode($re,JSON_UNESCAPED_UNICODE);
                return '';


        }
      $map=M('device')->where($where)->getField("shopid");///获取商家id
    

 

        if(empty($map)){
            $re['status']=3;
             $re['msg']="通过设备未绑定商家";
                echo json_encode($re,JSON_UNESCAPED_UNICODE);
                return '';

        }
		$landstate=M('shop_user')->where("shopid='%d'",$map)->getField('landstate');////判断往哪个设备上推送;

	
		if($landstate=='ios'){

			$channelId=M('shop_user')->where("shopid='%d'",$map)->getField('ichannelId');

			$result=$ios->index($itemid,$channelId);
            $res=serialize($result);
             $txt = '  '.date('Y-m-d H:i:s', time()).' uname='.$res.'|';
           file_put_contents("./Public/ios.text",$txt, FILE_APPEND);

		    var_dump($result);

	
		}
		if($landstate=='android')
		{
                
		$channelId=M('shop_user')->where("shopid='%d'",$map)->getField('achannelId');
        
		$result=$android->index($itemid,$channelId);

            $re=serialize($result);
             $txt = '  '.date('Y-m-d H:i:s', time()).' uname='.$re.'|';
           file_put_contents("./Public/android.text",$txt, FILE_APPEND);
		echo '<pre>';
		print_r($result);
		echo '</pre>';
		}
			
	  }
		}
		





